package cn.keniykey.mc.viewlibrary.view.api.template;

import java.util.List;

import javax.annotation.Nullable;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;

import cn.keniykey.mc.viewlibrary.container.api.EContainerType;
import cn.keniykey.mc.viewlibrary.view.api.AbFrameView;
import cn.keniykey.mc.viewlibrary.view.api.FrameViewBundle;
import cn.keniykey.mc.viewlibrary.view.api.IViewClickHandler;
import cn.keniykey.mc.viewlibrary.view.api.ViewClickReport;
import cn.keniykey.mc.viewlibrary.view.api.template.ChooseViewIntentBuilder.EChooseMenuViewIntentKey;

public abstract class AbChooseView extends AbFrameView{
	public static abstract class AbChooseMenuViewIconsGrid{
		protected abstract int getGridOfConfirm();
		protected abstract int getGridOfVeto();
		protected abstract int getGridOfCancel();
		protected abstract int getGridOfCountdown();
		protected abstract int getGridOfPrompt();
	}
	public enum EChooseResult{
		CONFIRM,VETO,CANCEL,UNKNOWN;
		
		public static EChooseResult muchResult(int result) {
			switch(result) {
			case 1: return CONFIRM;
			case 2: return VETO;
			case 3: return CANCEL;
			default:
			}
			return UNKNOWN;
		}
		public boolean isConfirm() {
			return this.equals(CONFIRM);
		}
	}
	public enum EChooseResultIntentKey {
		FORWARD;
	}
	
	private int slot_countdown;
	private ChooseViewCountdownRunnable runnable;
	
	protected AbChooseView(Plugin plugin1, Player player1) {
		super(plugin1, player1);
	}
	@Override
	protected void onCreateForResult(int requestcode) {
		super.onCreateForResult(requestcode);
		if(!this.getIntent().hasExtras()) throw new NullPointerException("Intent's Extras is NULL(没有给Intent传递设置Extras)");
		final FrameViewBundle ibundle = this.getIntent().getExtras();
		if(!ibundle.hasValue(EChooseMenuViewIntentKey.COUNTDOWN.toString())) throw new NullPointerException("Countdown not set(未设置倒计时)");
		final int countdown = ibundle.getValue(EChooseMenuViewIntentKey.COUNTDOWN.toString(), 1);
		if(countdown < 1 || countdown > 127) throw new RuntimeException("The set time should not be less than 1 or greater than 64(设置的时间不应小于1或大于64)");
		final AbChooseMenuViewIconsGrid iconsgrid = this.getDeafultIconsGrid();
		int slot_confirm,slot_veto,slot_cancel,slot_prompt;
		if(ibundle.hasValue(EChooseMenuViewIntentKey.CUSTOMGRID.toString())) {
			this.setViewType(ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMGRID_MENUTYPE.toString(), this.onDefaultEViewType()));
			slot_confirm = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMGRID_CONFIRM.toString(), iconsgrid.getGridOfConfirm());
			slot_veto = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMGRID_VETO.toString(), iconsgrid.getGridOfVeto());
			slot_cancel = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMGRID_CANCEL.toString(), iconsgrid.getGridOfCancel());
			this.slot_countdown = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMGRID_COUNTDOWN.toString(), iconsgrid.getGridOfCountdown());
			slot_prompt = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMGRID_PROMPT.toString(), iconsgrid.getGridOfPrompt());
		}else {
			this.setViewType(this.onDefaultEViewType());
			slot_confirm = iconsgrid.getGridOfConfirm();
			slot_veto = iconsgrid.getGridOfVeto();
			slot_cancel = iconsgrid.getGridOfCancel();
			this.slot_countdown = iconsgrid.getGridOfCountdown();
			slot_prompt = iconsgrid.getGridOfPrompt();
		}
		this.setTitle((ibundle.hasValue(EChooseMenuViewIntentKey.CUSTOM_TITLE.toString()) ?
				ibundle.getValue(EChooseMenuViewIntentKey.CUSTOM_TITLE.toString(),"ChooseView") : this.onDefaultTitle()));
		ItemStack item_confirm = null,item_veto= null,item_cancel = null,item_countdown = null,item_prompt = null;
		if(ibundle.hasValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY.toString())) {
			item_confirm = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY_CONFIRM.toString(), null);
			item_veto = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY_VETO.toString(), null);
			item_cancel = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY_CANCEL.toString(), null);
			item_countdown = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY_COUNTDOWN.toString(), null);
			item_prompt = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY_PROMPT.toString(), null);
		}else {
			item_confirm = this.onDeafultIconOfConfirm(ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY_CONFIRM.toString(), null));
			item_veto = this.onDeafultIconOfVeto(ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY_VETO.toString(), null));
			item_cancel = this.onDeafultIconOfCancel(ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY_CANCEL.toString(), null));
			item_countdown = this.onDeafultIconOfCountdown(ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY_COUNTDOWN.toString(), null));
			final String name = (String) ibundle.getValue(EChooseMenuViewIntentKey.CUSTOM_PROMPT_NAME.toString(),"Choose");
			final List<String> lore = ibundle.getValue(EChooseMenuViewIntentKey.CUSTOM_PROMPT_LORE.toString(), null);
			item_prompt = this.onDeafultIconOfPrompt(ibundle.getValue(EChooseMenuViewIntentKey.CUSTOMDISPLAY_PROMPT.toString(), null), name, lore);
		}
		this.setIcon(slot_prompt, item_prompt);
		this.setIconClick(slot_confirm, item_confirm, new IViewClickHandler() {
			@Override
			public void onClick(Player player,AbFrameView view, ViewClickReport report) {
				AbChooseView.this.runnable.cancel();
				AbChooseView.this.setResultAndIntent(1, null);
				view.finish();
			}
		});
		this.setIconClick(slot_veto, (item_veto == null ? this.onDeafultIconOfVeto(null) : item_veto), new IViewClickHandler() {
			@Override
			public void onClick(Player player,AbFrameView view, ViewClickReport report) {
				AbChooseView.this.runnable.cancel();
				AbChooseView.this.setResultAndIntent(2, null);
				view.finish();
			}
		});
		this.setIconClick(slot_cancel, (item_cancel == null ? this.onDeafultIconOfCancel(null) : item_cancel), new IViewClickHandler() {
			@Override
			public void onClick(Player player,AbFrameView view, ViewClickReport report) {
				AbChooseView.this.runnable.cancel();
				AbChooseView.this.setResultAndIntent(3, null);
				view.finish();
			}
		});
		if(item_countdown == null) item_countdown = this.onDeafultIconOfCountdown(null);
//		if(item_countdown.getType().getMaxStackSize() < countdown) throw new RuntimeException("Set time greater than item maximum stack(设置的时间大于物品最大堆叠)");
		item_countdown.setAmount(countdown);
		this.setIcon(this.slot_countdown, item_countdown);
		this.runnable = new ChooseViewCountdownRunnable();
		this.runnable.item_countdown1 = this.getContainer().getItemStack(this.slot_countdown);
	}
	private final class ChooseViewCountdownRunnable extends BukkitRunnable{
		private ItemStack item_countdown1;
		@Override
		public void run() {
			final int second = (this.item_countdown1.getAmount()-1);
			if(second > 0) {
				this.item_countdown1.setAmount(second);
				AbChooseView.this.refreshIcon(AbChooseView.this.slot_countdown);
				return;
			}
			if(AbChooseView.this.runnable.isCancelled()) return;
			AbChooseView.this.runnable.cancel();
			AbChooseView.this.setResultAndIntent(3, null);
			AbChooseView.this.finish();
		}
	}
	@Override
	protected void onShow() {
		super.onShow();
		this.runnable.runTaskTimerAsynchronously(this.getPlugin(), 20l, 20l);
	}
	@Override
	protected void onHidden() {
		super.onHidden();
		if(this.runnable.isCancelled()) return;
		this.runnable.cancel();
		this.setResultAndIntent(3, null);
	}
	@Override
	@Deprecated
	public void hidden() {
	}
	
	private void setResultAndIntent(int resultcode,@Nullable FrameViewBundle bundle) {
		if(this.getIntent().hasExtras()) {
			final FrameViewBundle extras = this.getIntent().getExtras();
			if(extras.hasValue(EChooseMenuViewIntentKey.FORWARD_CONTENT.toString())) {
				if(null == bundle) bundle = new FrameViewBundle();
				bundle.putValue(EChooseResultIntentKey.FORWARD.toString(), extras.getValue(EChooseMenuViewIntentKey.FORWARD_CONTENT.toString(), null));
			}
		}
		this.setResult(resultcode, bundle);
	}
	
	protected String onDefaultTitle() {
		return "ChooseView";
	}
	protected abstract EContainerType onDefaultEViewType();
	protected abstract AbChooseMenuViewIconsGrid getDeafultIconsGrid(); 
	protected abstract ItemStack onDeafultIconOfConfirm(@Nullable Material type);
	protected abstract ItemStack onDeafultIconOfVeto(@Nullable Material type);
	protected abstract ItemStack onDeafultIconOfCancel(@Nullable Material type);
	protected abstract ItemStack onDeafultIconOfCountdown(@Nullable Material type);
	protected abstract ItemStack onDeafultIconOfPrompt(@Nullable Material type,String name,@Nullable List<String> lore);
}